package org.wlgzs.office_exam_student.service;

import org.apache.commons.dbutils.handlers.MapHandler;
import org.wlgzs.office_exam_student.dao.BaseDao;
import org.wlgzs.office_exam_student.entity.Score;

import java.sql.*;
import java.util.Map;

/**
 * Copyright (C), 2016-2018, 孤城落寞的博客 FileName: ScoreService Author: gclm Date:
 * 2018/2/25 上午10:04 Description: ScoreService
 */
public class ScoreService extends BaseDao<Score> {

	// private int id;
	// private int userId;//考生id user_id
	// private String userName;//考生姓名
	// private int classesId;//班级id
	// private String classes;//班级名称
	// private int examId;//考试id
	// private String exam;//考试名称
	// private Timestamp startTime;//开始考试时间
	// private Timestamp endTome;//交卷时间
	// private int score;//成绩

	public int insert(Score score) {
		String sql = "insert into tb_score (user_id,user_name,classes_id,classes,exam_id,exam,start_time,end_time,score,choice_score)values(?,?,?,?,?,?,?,?,?,?)";
		return exec(qr -> {
			try {
				try(Connection conn = qr.getRealQR().getDataSource().getConnection()) {
                    PreparedStatement ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
                    ps.setInt(1, score.getUserId());
                    ps.setString(2, score.getUserName());
                    ps.setInt(3, score.getClassesId());
                    ps.setString(4, score.getClasses());
                    ps.setInt(5, score.getExamId());
                    ps.setString(6, score.getExam());
                    ps.setTimestamp(7, score.getStartTime());
                    ps.setTimestamp(8, score.getEndTime());
                    ps.setInt(9, score.getScore());
                    ps.setInt(10,score.getChoiceScore());
                    ps.executeUpdate();
                    ResultSet rs = ps.getGeneratedKeys();
                    int id = 0;
                    if(rs.next()){
                        id = rs.getInt(1);
                    }
                    return id;
                }
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		});
	}

	public void update(Score score) {
		String sql = "update tb_score set user_id = ? , user_name = ? , classes_id = ? , classes = ? , exam_id = ? , exam = ? , start_time = ? , end_time = ?" +
				" , score = ? , choice_score = ? , word_score = ? , excel_score=? , ppt_score = ? where id = ?";
		exec(qr -> {
			try {
				return qr.update(sql, score.getUserId(), score.getUserName(), score.getClassesId(), score.getClasses(),
						score.getExamId(), score.getExam(), score.getStartTime(), score.getEndTime(), score.getScore(),score.getChoiceScore()
						,score.getWordScore(),score.getExcelScore(),score.getPptScore(),score.getId());
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		});
	}

	public boolean hasScore(int userId, int examId) throws Exception {
		String sql = "select id from tb_score where user_id = ? and exam_id = ?";
		final SQLException[] es = new SQLException[1];
		boolean b = exec(qr -> {
			Map<String, Object> map;
			try {
				map = qr.query(sql, new MapHandler(),userId,examId);
				return  map == null? false:map.size() > 0;
			} catch (SQLException e) {
				
				es[0] = e;
			}
			return false;
		});
		if(es[0] == null) {
			return b;
		}
		throw es[0];
	}
}
